%macro do_merge;
		
	proc printto new log = "&logdir./do_merge.txt";
	run;
	
	/****************************
	*	Deal with receipts		*
	****************************/
	data receipts;
		merge rlib.receipts_sched_c(in=a)
			rlib.receipts_f1120(in=b)
			rlib.receipts_f1120s(in=c)
			rlib.receipts_f1065(in=d);
		by firm_id;
		
		receipts2020_master = .;
		if receipts2020_master = . then do;
			receipts2020_master = receipts2020_f1120;
			income2020_master = income2020_f1120;
		end;
		if receipts2020_master = . then do;
			receipts2020_master = receipts2020_f1120s;
			income2020_master = income2020_f1120s;
		end;
		if receipts2020_master = . then do;
			receipts2020_master = receipts2020_f1065;
			income2020_master = income2020_f1065;
		end;
		if receipts2020_master = . then do;
			receipts2020_master = receipts2020_sched_c;
			income2020_master = income2020_sched_c;
		end;
		
		/* entity type as of 2019 */
		receipts2019_master = .;
		if receipts2019_master = . then do;
			receipts2019_master = receipts2019_f1120;
			income2019_master = income2019_f1120;
			if income2019_f1120 ~= . then entity = 1;
		end;
		if receipts2019_master = . then do;
			receipts2019_master = receipts2019_f1120s;
			income2019_master = income2019_f1120s;
			if income2019_f1120s ~= . then entity = 2;
		end;
		if receipts2019_master = . then do;
			receipts2019_master = receipts2019_f1065;
			income2019_master = income2019_f1065;
			if income2019_f1065 ~= .  then entity = 4;
		end;
		if receipts2019_master = . then do;
			receipts2019_master = receipts2019_sched_c;
			income2019_master = income2019_sched_c;
			if income2019_sched_c ~= . then entity = 5;
		end;
				
		receipts2019_master = max(0, receipts2019_master);
		receipts2020_master = max(0, receipts2020_master);
			
		keep firm_id entity receipts2020_master receipts2019_master
			income2020_master income2019_master;
	run;
	
	/************************
	*		Amendments		*
	************************/
	proc means data = rlib.amendments nway noprint;
		class firm_id tp;
		output out = sum_amend(drop=_type_ _freq_)
			sum(ertc_amendment) = ertc_amendment;
	run;
	
	data inprog;
		merge rlib.f941 rlib.amendments_agg;
		by firm_id tp;
	run;
	
	/********************
	*	Merge in city	*
	********************/
	proc sort data = inprog;
		by zip;
	run;
	data inprog;
		merge inprog(in=a) rlib.zips;
		by zip;
		if a;
	run;
	proc sort data = inprog;
		by firm_id tp;
	run;
	
	
	/************************
	*	Everything else		*
	************************/

	
	
	data rlib.result;
		merge inprog(in=a) rlib.industry receipts(in=b) rlib.min_prd rlib.ppp;
		by firm_id;
		if a;

		
		prep_id = max(prepein, preptin);
		
		/* reset missings to zero */
		array nums[*] _numeric_;
		do j = 1 to dim(nums);
			if nums(j) = . then nums(j) = 0;
		end;
		drop j;
		
		/* delta_gr will be missing if not valid */
		if receipts2020_master > 0 then 
			delta_gr = (receipts2020_master - receipts2019_master)/receipts2019_master;
		
		drop prepein preptin receipts2020_master;
		
		
		bin = round(num_emp);
		if num_emp >= 50 then bin = floor(num_emp/5)*5+2.5; 
		if num_emp >= 150 then bin = floor(num_emp/10)*10+5;
		if num_emp >= 250 then bin = floor(num_emp/25)*25+12.5;
		if num_emp >= 600 then bin = floor(num_emp/50)*50+25;
		if num_emp >= 1200 then bin = floor(num_emp/100)*100 + 50;
		if num_emp >= 1500 then bin = floor(num_emp/250)*250 + 125;
		bin = min(3000,bin);
		
	run;
	
	
	proc printto;
	run;


%mend;